
package com.turing.draw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turing.common.constants.CommonConstants;
import com.turing.common.util.FileUtils;
import com.turing.common.util.Tools;
import com.turing.draw.dao.FileImageMapper;
import com.turing.draw.model.domain.FileImage;
import com.turing.draw.service.FileImageService;
import com.turing.draw.vo.out.PageSize;
import com.turing.orm.handler.DateTableNameParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 服务类实现
 * @Author lcy
 * @Date 2021-10-25
 */
@Service
public class FileImageServiceImpl extends ServiceImpl<FileImageMapper, FileImage> implements FileImageService {

    @Value("${fdfs.domain}")
    private String FDFS_FILE_DOMAIN;

    @Override
    public String getImageUrl(String childTaskSn, Integer pageNo) {
        DateTableNameParser.setTaskSn(Long.parseLong(childTaskSn));
        FileImage fileImage = lambdaQuery().eq(FileImage::getTaskSn, childTaskSn)
                .eq(FileImage::getPageNo, pageNo)
                .eq(FileImage::getIsDel, 0)
                .one();

        return Optional.ofNullable(fileImage).map(FileImage::getImageUrl).orElse(CommonConstants.EMPTY_STRING);

    }

    @Override
    public FileImage getFileImageByPage(String taskSn, Integer pageNo) {
        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        return lambdaQuery().eq( FileImage::getTaskSn, taskSn )
                .eq(FileImage::getPageNo, pageNo)
                .last("limit 1")
                .one();
    }

    @Override
    public boolean updateFileImage(String taskSn, Integer pageNo, String imageUrl,Integer width,Integer height) {
        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        return lambdaUpdate().eq( FileImage::getTaskSn, taskSn )
                .eq(FileImage::getPageNo,pageNo )
                .set(FileImage::getImageUrl, imageUrl)
                .set(FileImage::getWidth,width)
                .set(FileImage::getHeight,height)
                .update();
    }

    @Override
    public boolean saveJsonUrl(String taskSn, Integer pageNo, String pageJsonUrl) {
        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        return lambdaUpdate().eq( FileImage::getTaskSn, taskSn )
                .eq(FileImage::getPageNo,pageNo )
                .set(FileImage::getJsonUrl, pageJsonUrl)
                .update();

    }

    @Override
    public JSONObject getPageJson(String taskSn, Integer pageNo) {
        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        FileImage fileImage = lambdaQuery().eq(FileImage::getTaskSn, taskSn)
                .eq(FileImage::getPageNo, pageNo)
                .select(FileImage::getJsonUrl)
                .last("limit 1").one();
        if(fileImage == null ){
            return new JSONObject();
        }
        String jsonUrl = FDFS_FILE_DOMAIN + fileImage.getJsonUrl();
        String jsonStr = FileUtils.loadJson(jsonUrl);
        return JSONObject.parseObject(jsonStr);
    }

    @Override
    public Map<Integer,JSONObject> batchGetPageJson(String taskSn, List<Integer> pages) {

        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        List<FileImage> fileImages = lambdaQuery().eq(FileImage::getTaskSn, taskSn)
                .in(FileImage::getPageNo, pages)
                .select(FileImage::getPageNo,FileImage::getJsonUrl)
                .orderByAsc(FileImage::getPageNo).list();
        if(Tools.isEmpty(fileImages)){
            return new HashMap<>();
        }
        return fileImages.stream().collect(Collectors.toMap(FileImage::getPageNo, fileImage ->
                JSONObject.parseObject(FileUtils.loadJson(FDFS_FILE_DOMAIN + fileImage.getJsonUrl()))));

    }

    @Override
    public List<FileImage> getFileImageByPages(String taskSn, List<Integer> pages) {

        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        return lambdaQuery().eq( FileImage::getTaskSn, taskSn )
                .in(FileImage::getPageNo, pages)
                .list();

    }

    @Override
    public boolean saveFileImage(String parentTaskSn, String taskSn, Integer pageNo, String imageUrl, Integer width, Integer height) {

        FileImage fileImage = FileImage.builder().parentTaskSn(parentTaskSn)
                .taskSn(taskSn)
                .pageNo(pageNo)
                .imageUrl(imageUrl)
                .width(width)
                .height(height).build();
        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        return save(fileImage);
    }

    @Override
    public List<PageSize> getPagesSize(String taskSn) {

        DateTableNameParser.setTaskSn(Long.parseLong(taskSn));
        List<FileImage> fileImages = lambdaQuery().eq(FileImage::getTaskSn, taskSn).orderByAsc(FileImage::getPageNo).list();
        if(Tools.isEmpty(fileImages)){
            return new ArrayList<>();
        }
        return fileImages.stream().map(fi-> PageSize.builder().pageNo(fi.getPageNo()).width(fi.getWidth()).height(fi.getHeight()).build()).collect(Collectors.toList());

    }

}
